package com.sky.demo.bitmap;

import java.io.InputStream;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.ColorMatrix;
import android.graphics.ColorMatrixColorFilter;
import android.graphics.Paint;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
/**
 * @author 彬 QQ 1136096189
 * @Description: bitmap工具类
 * @date 2015/8/17 15:30
 */
public class ImageUtils {

    /**
     * 对图片进行压缩处理
     *
     * @param context
     * @param resId
     * @return
     */
    public static Bitmap readBitMap(Context context, int resId) {
        BitmapFactory.Options opt = new BitmapFactory.Options();
        opt.inPreferredConfig = Bitmap.Config.RGB_565;
        opt.inPurgeable = true;
        opt.inInputShareable = true;
        // 获取资源图片
        InputStream is = context.getResources().openRawResource(resId);
        Bitmap bitmap = BitmapFactory.decodeStream(is, null, opt);
        return bitmap;
    }/**
     * 对图片进行压缩处理
     *
     * @param context
     * @param resId
     * @return
     */
    public static Bitmap reSizeBitMap(Context context,String pathName, int resId) {
        BitmapFactory.Options opt = new BitmapFactory.Options();
        opt.inJustDecodeBounds = true;
        BitmapFactory.decodeFile(pathName,opt);
        double radio = Math.max(opt.outWidth*1.0d/1024,opt.outHeight*1.0d/1024);
        opt.inSampleSize= (int) Math.ceil(radio);
        opt.inJustDecodeBounds = false;
        Bitmap bitmap = BitmapFactory.decodeFile(pathName, opt);;

        // 获取资源图片
//        InputStream is = context.getResources().openRawResource(resId);
//        Bitmap bitmap = BitmapFactory.decodeStream(is, null, opt);
        return bitmap;
    }

    /**
     * bitmap转换为drawable
     *
     * @param context
     * @param toBitmap
     * @return 转换后的drawable
     */
    public static Drawable bitmapToDrawable(Context context, Bitmap toBitmap) {
        Drawable d = new BitmapDrawable(context.getResources(), toBitmap);
        return d;
    }

    public static void recyleBit(Bitmap bitmap) {
        if (!bitmap.isRecycled()) {// 先判断图片是否已释放了
            bitmap.recycle();
        }
    }

    /**
     * 创建新图片
     *
     * @param bitmap     图片
     * @param hue        色相
     * @param lum        亮度
     * @param saturation 饱和度
     * @return bmp
     *
     */
    public Bitmap creatNewBitmap(Bitmap bitmap, float hue, float saturation, float lum) {
        Bitmap bmp = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), Bitmap.Config.ARGB_8888);
        Canvas canvas = new Canvas(bmp);
        Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
        ColorMatrix hueMatrix = new ColorMatrix();
        hueMatrix.setRotate(0, hue);
        hueMatrix.setRotate(1, hue);
        hueMatrix.setRotate(2, hue);
        ColorMatrix saturationMatrix = new ColorMatrix();
        saturationMatrix.setSaturation(saturation);
        ColorMatrix lumMatrix = new ColorMatrix();
        lumMatrix.setScale(lum, lum, lum, 1);
        ColorMatrix imageMatrix = new ColorMatrix();
        imageMatrix.postConcat(hueMatrix);
        imageMatrix.postConcat(saturationMatrix);
        imageMatrix.postConcat(lumMatrix);
        paint.setColorFilter(new ColorMatrixColorFilter(imageMatrix));
        canvas.drawBitmap(bmp, 0, 0, paint);
        return bmp;
    }

    /**
     * 创建底片效果的图片
     * @param bitmap 图片
     * @return bmp
     */
    public static Bitmap handlerImageNegative(Bitmap bitmap) {
        int width = bitmap.getWidth();
        int height = bitmap.getHeight();
        Bitmap bmp = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), Bitmap.Config.ARGB_8888);
        int[] oldPx = new int[width * height];
        bitmap.getPixels(oldPx, 0, width, 0, 0, width, height);
        int r, g, b, a;
        int color;
        int[] newPx = new int[width * height];
        for (int i = 0; i < width * height; i++) {
            color = oldPx[i];
            r = Color.red(color);
            g = Color.green(color);
            b = Color.blue(color);
            a = Color.alpha(color);

            r = 255 - r;
            g = 255 - g;
            b = 255 - b;
            if (r > 255) {
                r = 255;
            } else if (r < 0) {
                r = 0;
            }
            if (g > 255) {
                g = 255;
            } else if (g < 0) {
                g = 0;
            }

            if (b > 255) {
                b = 255;
            } else if (b < 0) {
                b = 0;
            }
            newPx[i] = Color.argb(a,r, g, b);
        }
        bmp.setPixels(newPx, 0, width, 0, 0, width, height);
        return bmp;
    }
    /**
     * 创建怀旧效果的图片
     * @param bitmap 图片
     * @return bmp
     */
    public static Bitmap handlerImageOldPhoto(Bitmap bitmap) {
        int width = bitmap.getWidth();
        int height = bitmap.getHeight();
        Bitmap bmp = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), Bitmap.Config.ARGB_8888);
        int[] oldPx = new int[width * height];
        bitmap.getPixels(oldPx, 0, width, 0, 0, width, height);
        int r, g, b, a;
        int r1, g1, b1, a1;
        int color;
        int[] newPx = new int[width * height];
        for (int i = 0; i < width * height; i++) {
            color = oldPx[i];
            r = Color.red(color);
            g = Color.green(color);
            b = Color.blue(color);
            a = Color.alpha(color);

            r1 = (int) (0.393*r+0.769*g+0.189*b);
            g1 = (int) (0.349*r+0.686*g+0.168*b);
            b1 = (int) (0.272*r+0.534*g+0.131*b);
            if (r1 > 255) {
                r1 = 255;
            } else if (r1 < 0) {
                r1 = 0;
            }
            if (g1 > 255) {
                g1 = 255;
            } else if (g1 < 0) {
                g1 = 0;
            }

            if (b1 > 255) {
                b1 = 255;
            } else if (b1 < 0) {
                b1 = 0;
            }
            newPx[i] = Color.argb(a,r1, g1, b1 );
        }
        bmp.setPixels(newPx, 0, width, 0, 0, width, height);
        return bmp;
    }
    /**
     * 创建浮雕效果的图片
     * @param bitmap 图片
     * @return bmp
     */
    public static Bitmap handlerImagePixelsrelier(Bitmap bitmap) {
        int width = bitmap.getWidth();
        int height = bitmap.getHeight();
        Bitmap bmp = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), Bitmap.Config.ARGB_8888);
        int[] oldPx = new int[width * height];
        bitmap.getPixels(oldPx, 0, width, 0, 0, width, height);
        int r, g, b, a;
        int r1, g1, b1, a1;
        int color,colorBefore;
        int[] newPx = new int[width * height];
        for (int i = 1; i < width * height; i++) {
            colorBefore = oldPx[i-1];
            r = Color.red(colorBefore);
            g = Color.green(colorBefore);
            b = Color.blue(colorBefore);
            a = Color.alpha(colorBefore);

            color = oldPx[i];
            r1 = Color.red(color);
            g1 = Color.green(color);
            b1 = Color.blue(color);
            a1= Color.alpha(color);

            r = r-r1+127;
            g = g-g1+127;
            b = b-b1+127;
            if (r > 255) {
                r = 255;
            } else if (r < 0) {
                r = 0;
            }
            if (g > 255) {
                g = 255;
            } else if (g < 0) {
                g = 0;
            }

            if (b > 255) {
                b = 255;
            } else if (b < 0) {
                b = 0;
            }
            newPx[i] = Color.argb(a,r, g, b);
        }
        bmp.setPixels(newPx, 0, width, 0, 0, width, height);
        return bmp;
    }

}
